package stravel
import org.springframework.security.access.annotation.Secured

@Secured('permitAll')
class ReviewController {

	def index(Long id) {
		def hotelInstance = Hotel.get(id)
		if(hotelInstance){
			HotelViewCounter hotelViewCounter = new HotelViewCounter(hotel:hotelInstance, viewDate:new Date())
			hotelViewCounter.save(flush:true)
			[hotelInstance:hotelInstance]
		}else{
			redirect(controller:'home')
		}
	}

	def state(Long id) {
		def stateInstance = State.get(id)
		[stateInstance:stateInstance]
	}

	def city(Long id) {
		def cityInstance = City.get(id)
		[cityInstance:cityInstance]
	}

	def country(Long id) {
		def countryInstance = Country.get(id)
		[countryInstance:countryInstance]
	}

	def rate(Long id, Integer rate){
		def hotelInstance = Hotel.get(id)
		BigDecimal vot = hotelInstance.rate
		Integer tot = hotelInstance.noOfVote
		if(vot == null){
			vot = 0
		}
		if(tot == null){
			tot = 0
		}
		BigDecimal prevTot = vot*tot
		BigDecimal curTot = prevTot+rate
		tot = tot +1
		vot = curTot/tot
		hotelInstance.rate = vot
		hotelInstance.noOfVote = tot
		hotelInstance.save(flush:true)
		println "submt raate ${id} ${rate}"
		redirect(action:'index', id:id)
	}
}
